package com.sgb.knowledgeshare.weight;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.sgb.knowledgeshare.R;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

/**
 * Created by fq on 2020/9/11 0011
 * Description：分享-录制视频的view
 */
public class RecordVideoView extends RelativeLayout {

    private RecordVideoViewListener recordvideoListener;

    private ImageView sharCamera, shareLvGing, shareDJS, sharSGD, shareZanTing, shareShanChu, shareGouXuan;
    private TextView maxTime, minTime,shareRecordTimes;
    private RecordTimelineView mRecordTimeView;
    private RelativeLayout rlBack;
    //最小录制时长
    private int minRecordTime = 6*1000;
    //最大录制时长
    private int maxRecordTime = 60 * 1000;
    private LinearLayout llRight,llTime,sharePhoto;

    public int getMaxRecordTime() {
        return maxRecordTime;
    }

    public boolean RecordClick =true;
    boolean LightSwitch =true;
    private AlivcCountDownView alivccountdownviews;
    private boolean AlivcCountDown = true;
    private boolean WhetherStartRecord = false;

    public RecordVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.record_video_view, this, true);
        //获取布局控件信息
        assignViews();
        //给view设置点击事件
        setViewListener();
    }


    private void assignViews() {
        sharCamera = (ImageView) findViewById(R.id.share_camera); //相机
        shareLvGing = (ImageView) findViewById(R.id.share_lvjing);//滤镜
        shareDJS = (ImageView) findViewById(R.id.share_djs);//倒计时
        sharSGD = (ImageView) findViewById(R.id.share_sgd);//闪光灯
        shareZanTing = (ImageView) findViewById(R.id.share_zanting);//暂停、录制
        shareShanChu = (ImageView) findViewById(R.id.share_shanchu);//删除
        shareGouXuan = (ImageView) findViewById(R.id.share_gouxuan);//报存
        maxTime = (TextView) findViewById(R.id.max_time);//最大事件
        minTime = (TextView) findViewById(R.id.min_time);//最小时间
        rlBack = (RelativeLayout) findViewById(R.id.rl_back);//最小时间
        llRight = (LinearLayout) findViewById(R.id.ll_right);//右边菜单布局
        llTime = (LinearLayout) findViewById(R.id.ll_time);//时间选择布局
        sharePhoto = (LinearLayout) findViewById(R.id.share_photo);//相册选择弹窗
        alivccountdownviews = (AlivcCountDownView) findViewById(R.id.alivccountdownview);//倒计时布局
        mRecordTimeView = (RecordTimelineView) findViewById(R.id.recordtimelineview);//进度条布局
        shareRecordTimes = (TextView) findViewById(R.id.record_video_times);//录制时间
        mRecordTimeView.setColor(R.color.color_5792FD, R.color.color_0000CC, R.color.alivc_common_white,
                R.color.color_33000000);
        mRecordTimeView.setMinDuration(minRecordTime);
        mRecordTimeView.setMaxDuration(maxRecordTime);
        alivccountdownviews.setOnCountDownFinishListener(new AlivcCountDownView.OnCountDownFinishListener() {
            @Override
            public void onFinish() {
                shareShanChu.setVisibility(GONE);
                shareGouXuan.setVisibility(GONE);
                initViewGone();
                WhetherStartRecord = true;
                recordvideoListener.onStartRecordClick();
                RecordClick=false;
            }
        });
    }

    private void initViewVisible() {
        shareZanTing.setImageResource(R.mipmap.zanting);
        llRight.setVisibility(VISIBLE);
        llTime.setVisibility(VISIBLE);
        rlBack.setVisibility(VISIBLE);
       // shareRecordTimes.setVisibility(VISIBLE);
    }

    private void initViewGone() {
        shareZanTing.setImageResource(R.mipmap.paishezhong);
        llRight.setVisibility(GONE);
        llTime.setVisibility(GONE);
        rlBack.setVisibility(GONE);
      // shareRecordTimes.setVisibility(GONE);
    }

    public void setmRecordTime(long mRecordTime) {
        this.mRecordTimeView.setDuration((int) mRecordTime);
    }

    public void setshareRecordTimes(String RecordTime) {
        this.shareRecordTimes.setText(RecordTime);
    }

    public RecordTimelineView getRecordTimeView() {
        return mRecordTimeView;
    }

    private void setViewListener() {

//        返回上级
        rlBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (recordvideoListener != null) {
                    recordvideoListener.onBackClick();
                }
            }
        });
        //  摄像头转换
        sharCamera.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))){
                    if (SharedPreferenceUtil.getInstance().getSaveBooleanData(SharedPreferenceUtil.PERSON_AUTH,false)){
                        if (recordvideoListener != null) {
                            recordvideoListener.onCameraSwitch();
                        }
                    }else {
                        MToast.showToast(getContext(),"请实名认证完成后再进行录制发布！");
                    }
                }else {
                    MToast.showToast(getContext(),"请先登录账户！");
                }
            }
        });
        //  滤镜
        shareLvGing.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))){
                    if (SharedPreferenceUtil.getInstance().getSaveBooleanData(SharedPreferenceUtil.PERSON_AUTH,false)){
                        if (recordvideoListener != null) {
                            recordvideoListener.onCameraSwitch();
                        }
                    }else {
                        MToast.showToast(getContext(),"请实名认证完成后再进行录制发布！");
                    }
                }else {
                    MToast.showToast(getContext(),"请先登录账户！");
                }
            }
        });
        //  倒计时
        shareDJS.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))){
                    if (SharedPreferenceUtil.getInstance().getSaveBooleanData(SharedPreferenceUtil.PERSON_AUTH,false)){
                        if (WhetherStartRecord==false){
                            if (AlivcCountDown){
                                alivccountdownviews.start();
                                AlivcCountDown=false;
                                if (recordvideoListener != null) {
                                    recordvideoListener.onReadyRecordClick(true);
                                }
                            }else {
                                alivccountdownviews.cancle();
                                AlivcCountDown=true;
                            }
                        }else {
                            MToast.showToast(getContext(),"已开始录制不能使用此功能！");
                        }
                    }else {
                        MToast.showToast(getContext(),"请实名认证完成后再进行录制发布！");
                    }
                }else {
                    MToast.showToast(getContext(),"请先登录账户！");
                }
            }
        });
        //  闪光灯
        sharSGD.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))){
                    if (SharedPreferenceUtil.getInstance().getSaveBooleanData(SharedPreferenceUtil.PERSON_AUTH,false)){
                        if (recordvideoListener != null) {
                            if (LightSwitch){
                                sharSGD.setBackgroundResource(R.mipmap.open_sgd);
                                recordvideoListener.onLightSwitch(LightSwitch);
                                LightSwitch=false;
                            }else {
                                sharSGD.setBackgroundResource(R.mipmap.close_sgd);
                                recordvideoListener.onLightSwitch(LightSwitch);
                                LightSwitch=true;
                            }
                        }
                    }else {
                        MToast.showToast(getContext(),"请实名认证完成后再进行录制发布！");
                    }
                }else {
                    MToast.showToast(getContext(),"请先登录账户！");
                }
            }
        });
        // 暂停和录制
        shareZanTing.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))){
                    if (SharedPreferenceUtil.getInstance().getSaveBooleanData(SharedPreferenceUtil.PERSON_AUTH,false)){
                        if (recordvideoListener != null) {
                            if (RecordClick){
                                shareShanChu.setVisibility(GONE);
                                shareGouXuan.setVisibility(GONE);
                                WhetherStartRecord = true;
                                sharePhoto.setVisibility(GONE);
                                recordvideoListener.onStartRecordClick();
                                RecordClick=false;
                                initViewGone();
                            }else {
                                initViewVisible();
                                shareShanChu.setVisibility(VISIBLE);
                                shareGouXuan.setVisibility(VISIBLE);
                                recordvideoListener.onStopRecordClick();
                                RecordClick = true;
                                sharePhoto.setVisibility(GONE);
                            }
                        }
                    }else {
                        MToast.showToast(getContext(),"请实名认证完成后再进行录制发布！");
                    }
                }else {
                    MToast.showToast(getContext(),"请先登录账户！");
                }
            }
        });
        //  删除录制的视频
        shareShanChu.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))){
                    if (SharedPreferenceUtil.getInstance().getSaveBooleanData(SharedPreferenceUtil.PERSON_AUTH,false)){
                        if (recordvideoListener != null) {
                            recordvideoListener.onDeleteClick(sharePhoto,shareShanChu,shareGouXuan);
                        }
                    }else {
                        MToast.showToast(getContext(),"请实名认证完成后再进行录制发布！");
                    }
                }else {
                    MToast.showToast(getContext(),"请先登录账户！");
                }

            }
        });
        //  保存录制的视频下一步
        shareGouXuan.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))){
                    if (SharedPreferenceUtil.getInstance().getSaveBooleanData(SharedPreferenceUtil.PERSON_AUTH,false)){
                        if (recordvideoListener != null) {
                            recordvideoListener.onNextClick();
                        }
                    }else {
                        MToast.showToast(getContext(),"请实名认证完成后再进行录制发布！");
                    }
                }else {
                    MToast.showToast(getContext(),"请先登录账户！");
                }
            }
        });
//        3分钟时长
        maxTime.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
               /* if (!TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))){
                    if (SharedPreferenceUtil.getInstance().getSaveBooleanData(SharedPreferenceUtil.PERSON_AUTH,false)){
                        maxTime.setTextColor(getResources().getColor(R.color.white));
                        minTime.setTextColor(getResources().getColor(R.color.color_969696));
                        maxRecordTime = 3*60*1000;
                        mRecordTimeView.setMaxDuration(maxRecordTime);
                    }else {
                        MToast.showToast(getContext(),"请实名认证完成后再进行录制发布！");
                    }
                }else {
                    MToast.showToast(getContext(),"请先登录账户！");
                }*/
            }
        });
//        60秒时长
        minTime.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))){
                    if (SharedPreferenceUtil.getInstance().getSaveBooleanData(SharedPreferenceUtil.PERSON_AUTH,false)){
                        maxRecordTime = 60 * 1000;
                        maxTime.setTextColor(getResources().getColor(R.color.color_tab_three));
                        minTime.setTextColor(getResources().getColor(R.color.white));
                        mRecordTimeView.setMaxDuration(maxRecordTime);
                    }else {
                        MToast.showToast(getContext(),"请实名认证完成后再进行录制发布！");
                    }
                }else {
                    MToast.showToast(getContext(),"请先登录账户！");
                }
            }
        });
        //选择弹窗处理
        sharePhoto.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                recordvideoListener.onVideoPhoto();
            }
        });
    }

    /**
     * 给view添加事件
     *
     * @param recordvideoListener
     */
    public void setRecordvideoListener(RecordVideoViewListener recordvideoListener) {
        this.recordvideoListener = recordvideoListener;
    }

    public void setViewVisible() {
        initViewVisible();
        shareShanChu.setVisibility(VISIBLE);
        shareGouXuan.setVisibility(VISIBLE);
        recordvideoListener.onCompleteRecordClick();
        RecordClick = true;
        //取消点击事件
        shareZanTing.setOnClickListener(null);
    }
}
